# coding=utf-8

from mysql import UsingMysql
import re

class Disease_Model:
    '''
    Google-丁香疾病图片数据库
    disease Model
    @author spring
    @date 2022.02.24
    '''

    # id = 0
    #
    # dx_id = ''
    #
    # index = ''
    #
    # name = ''
    #
    # name_en = ''
    #
    # department= ''
    #
    # status = 0
    #
    # nums = 0

    #表名
    __table_name = 'ai_google_disease'

    def __init__(self) -> None:
        pass

    # def __init__(self, id, dx_id, index, name, name_en, department, status, nums):
    #
    #     """
    #     初始化
    #     """
    #     self.id  = id
    #     self.dx_id  = dx_id
    #     self.index  = index
    #     self.name  = name
    #     self.name_en  = name_en
    #     self.department  = department
    #     self.status  = status
    #     self.nums  = nums

    #def __str__(self) -> str:
        #return self.serial_id + ',' + self.pharmacy_name + ',' + self.pilot_area

    def save(self):
        """
        保存
        """
        data = self.get(self.id)
        if data:
            self.update()
        else:
            self.insert()

    def get(self, id):
        """
        获取数据
        """
        with UsingMysql(log_time=True) as um:
            um.cursor.execute("SELECT * from `"+self.__table_name+"` WHERE `id`="+id)
            return um.cursor.fetchone()

    def fetch(self, id, limit=""):
        """
        获取数据
        """
        with UsingMysql(log_time=True) as um:
            um.cursor.execute("SELECT * from `" + self.__table_name + "` WHERE `id`>" + id + " LIMIT "+limit)
            return um.cursor.fetchall()

    def update(self, id, status):
        '''
        编辑数据
        '''
        with UsingMysql(log_time=True) as um:

            sql = (
                "UPDATE `"+self.__table_name +"` SET `status`=%s "
                " WHERE `id`=%s"
            )
            sql = sql % (um._conn.escape(status),
                         um._conn.escape(id))
            um.cursor.execute(sql)
                                
            return um._conn.affected_rows
